package com.kk.netty.basice.demo2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Date;

public class ChannelFutureTest {
    public static void main(String[] args) throws InterruptedException {
        ChannelFuture channelFuture = new Bootstrap ( )
                .group (new NioEventLoopGroup ( ))
                .channel (NioSocketChannel.class)
                .handler (new ChannelInitializer<Channel> ( ) {
                    @Override
                    protected void initChannel(Channel ch) {
                        ch.pipeline ( ).addLast (new StringEncoder ( ));
                    }
                })
                .connect ("127.0.0.1", 8080);
        //channelFuture.sync ( ).channel ( ).writeAndFlush (new Date ( ) + "hello!");
        channelFuture.channel ( ).writeAndFlush (new Date ( ) + "hello!");

        System.out.println (channelFuture.channel ( ));
//        channelFuture.sync ();
//        System.out.println (channelFuture.channel () );

        channelFuture.addListener ((ChannelFutureListener) future -> {
            System.out.println (future.channel ( ));
        });

    }
}
